/**
 * Created by gouyuxiu on 2017/6/7.
 */
$.fn.myScollerLeftOrRight = function(){
    var $this = this;
    var $target = this.siblings(".navs");

    var index = parseInt($($target.find(".one-nav:not(.hidden)")[0]).attr("data-index"));
    var totalWidth = $target.width();
    var width = $($target.find(".one-nav:not(.hidden)")[0]).width();
    var total = Math.floor(totalWidth/width) + $target.find(".one-nav.hidden").length;
    var len = $target.children().length;

    var move = len - total;

    if($this.hasClass("disabled")){
        return;
    }else{
        if($this.hasClass("left-arrow")){
            $target.find(".one-nav[data-index="+((index-1).toString())+"]").removeClass("hidden");
            if(index == 1){
                $this.siblings(".right-arrow").removeClass("disabled");
                $this.addClass("disabled");
            }
            if($target.find(".one-nav[data-index="+index+"]").hasClass("active")){
                $target.find(".one-nav[data-index="+index+"]").removeClass("active");
                $target.find(".one-nav[data-index="+(index-1)+"]").addClass("active");
            }else if($target.find(".one-nav[data-index="+(total - index)+"]").hasClass("active")){
                $target.find(".one-nav[data-index="+(total - index)+"]").removeClass("active");
                $target.find(".one-nav[data-index="+(total - index - 1)+"]").addClass("active");
            }
        }else if($this.hasClass("right-arrow")){
            $target.find(".one-nav[data-index="+(index)+"]").addClass("hidden");
            if(move == 1 ) {
                $this.siblings(".left-arrow").removeClass("disabled");
                $this.addClass("disabled");
            }
            if($target.find(".one-nav[data-index="+index+"]").hasClass("active")){
                $target.find(".one-nav[data-index="+index+"]").removeClass("active");
                $target.find(".one-nav[data-index="+(index+1)+"]").addClass("active");
            }
        }
    }
}

$.fn.myDisabled = function(){
    this.find(".one-nav.hidden").removeClass("hidden");
    var width = $(this.find(".one-nav:not(.hidden)")[0]).width();
    var totalWidth = this.width();
    var total = Math.floor(totalWidth/width);
    var len = this.children().length;
    if(total == len){
        this.siblings(".left-arrow").addClass("disabled");
        this.siblings(".right-arrow").addClass("disabled");
    }else{
        this.siblings(".left-arrow").addClass("disabled");
        this.siblings(".right-arrow").removeClass("disabled");
    }
}

$(function(){
    console.log(111);
    $(".navs").myDisabled();

    $(".left-arrow").click(function(){
        $(this).myScollerLeftOrRight();
    });
    $(".right-arrow").click(function(){
        $(this).myScollerLeftOrRight();
    });

    $(window).resize(function(){
        $(".navs").myDisabled();
    });

    $(".navs .one-nav").click(function(){
        $(".one-nav.active").removeClass("active");
        $(this).addClass("active");
    });
});
